project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m
branchios-revival
changeset 11138 05853b1df2a2
parent 11137 14f50dde3e8c
child 11148 064a53861759
equal deleted inserted replaced
11137:14f50dde3e8c 11138:05853b1df2a2
    22 #import <QuartzCore/QuartzCore.h>
    22 #import <QuartzCore/QuartzCore.h>
    23 
    23 
    24 
    24 
    25 #define INDICATOR_TAG 7654
    25 #define INDICATOR_TAG 7654
    26 
    26 
       
    27 @interface MapPreviewButtonView ()
       
    28 @property (nonatomic) int port;
       
    29 @end
       
    30 
    27 @implementation MapPreviewButtonView
    31 @implementation MapPreviewButtonView
    28 @synthesize delegate;
    32 @synthesize delegate;
    29 
    33 
    30 -(id) initWithFrame:(CGRect)frame {
    34 -(id) initWithFrame:(CGRect)frame {
    31     if ((self = [super initWithFrame:frame])) {
    35     if ((self = [super initWithFrame:frame])) {
    62 
    66 
    63 -(void) engineProtocol:(uint8_t *)unpackedMap {
    67 -(void) engineProtocol:(uint8_t *)unpackedMap {
    64     IPaddress ip;
    68     IPaddress ip;
    65     BOOL serverQuit = NO;
    69     BOOL serverQuit = NO;
    66     uint8_t packedMap[128*32];
    70     uint8_t packedMap[128*32];
    67     int port = [HWUtils randomPort];
    71     self.port = [HWUtils randomPort];
    68 
    72 
    69     if (SDLNet_Init() < 0) {
    73     if (SDLNet_Init() < 0) {
    70         DLog(@"SDLNet_Init: %s", SDLNet_GetError());
    74         DLog(@"SDLNet_Init: %s", SDLNet_GetError());
    71         serverQuit = YES;
    75         serverQuit = YES;
    72     }
    76     }
    73 
    77 
    74     // Resolving the host using NULL make network interface to listen
    78     // Resolving the host using NULL make network interface to listen
    75     if (SDLNet_ResolveHost(&ip, NULL, port) < 0) {
    79     if (SDLNet_ResolveHost(&ip, NULL, self.port) < 0) {
    76         DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
    80         DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
    77         serverQuit = YES;
    81         serverQuit = YES;
    78     }
    82     }
    79 
    83 
    80     // Open a connection with the IP provided (listen on the host's port)
    84     // Open a connection with the IP provided (listen on the host's port)
    81     if (!(sd = SDLNet_TCP_Open(&ip))) {
    85     if (!(sd = SDLNet_TCP_Open(&ip))) {
    82         DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
    86         DLog(@"SDLNet_TCP_Open: %s %d\n", SDLNet_GetError(), self.port);
    83         serverQuit = YES;
    87         serverQuit = YES;
    84     }
    88     }
    85 
    89 
    86     // launch the preview in background here so that we're sure the tcp channel is open
    90     // launch the preview in background here so that we're sure the tcp channel is open
    87     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
    91     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
    88         NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", port];
    92         NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.port];
    89         NSString *documentsDirectory = DOCUMENTS_FOLDER();
    93         NSString *documentsDirectory = DOCUMENTS_FOLDER();
    90         
    94         
    91         NSMutableArray *gameParameters = [[NSMutableArray alloc] initWithObjects:
    95         NSMutableArray *gameParameters = [[NSMutableArray alloc] initWithObjects:
    92                                           @"--internal",
    96                                           @"--internal",
    93                                           @"--port", ipcString,
    97                                           @"--port", ipcString,
   108         for (int i = 0; i < argc; i++)
   112         for (int i = 0; i < argc; i++)
   109             free((void *)argv[i]);
   113             free((void *)argv[i]);
   110         free(argv);
   114         free(argv);
   111     });
   115     });
   112     
   116     
   113     DLog(@"Waiting for a client on port %d", port);
   117     DLog(@"Waiting for a client on port %d", self.port);
   114     while (!serverQuit) {
   118     while (!serverQuit) {
   115         /* This check the sd if there is a pending connection.
   119         /* This check the sd if there is a pending connection.
   116          * If there is one, accept that, and open a new socket for communicating */
   120          * If there is one, accept that, and open a new socket for communicating */
   117         csd = SDLNet_TCP_Accept(sd);
   121         csd = SDLNet_TCP_Accept(sd);
   118         if (NULL != csd) {
   122         if (NULL != csd) {
   131 
   135 
   132             SDLNet_TCP_Close(csd);
   136             SDLNet_TCP_Close(csd);
   133             serverQuit = YES;
   137             serverQuit = YES;
   134         }
   138         }
   135     }
   139     }
   136     [HWUtils freePort:port];
   140     [HWUtils freePort:self.port];
   137     SDLNet_TCP_Close(sd);
   141     SDLNet_TCP_Close(sd);
   138     SDLNet_Quit();
   142     SDLNet_Quit();
   139 
   143 
   140     // spread the packed bits in an array of bytes (one pixel per element, 0= transparent 1= color)
   144     // spread the packed bits in an array of bytes (one pixel per element, 0= transparent 1= color)
   141     int k = 0;
   145     int k = 0;