Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Add new function to libcod on windows

  1. #1
    Private
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    6
    Thanked 5 Times in 1 Post

    Add new function to libcod on windows

    Hey there,

    I want to add my own function to libcod (win version). Its an udp socket communication stuff, and its working in a c console application. But after I copied source inside the dll source, there is no error I can compile it, its not working. Socket is fine I can store data on it from gsc and the other side get it, but the receiver function not working. i = recvfrom(Socket, response, strlen(response), 0, 0, 0); (Socket is the socket and the response is a char array)i value is -1 after the call (i should represent the number of the bytes received), like I sad the console app stuff working fine with the same source, so I guess the source is fine. Any idea?

    Thanks in advance!

  2. #2
    Global Mossaderator Mitch's Avatar
    Join Date
    Nov 2012
    Posts
    654
    Thanks
    204
    Thanked 450 Times in 305 Posts
    Can you post your changes? I can't say what is wrong with so little code. You could use Wireshark to see if your message gets send.
    Also you could try debugging your function with printf (for Windows you need Com_printf).

    If you haven't added your own function yet see this thread: http://killtube.org/showthread.php?2...ibcod-function

  3. The Following User Says Thank You to Mitch For This Useful Post:

    Kantin (9th January 2015)

  4. #3
    Private
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    6
    Thanked 5 Times in 1 Post
    Sure i can, so i append
    PHP Code:
    {"foo"             gsc_utils_foo             0}, 
    to the Scr_Function scriptFunctions[] array in gsc.cpp, I added also the prototype to the header file.

    and this to gsc_utils.cpp
    PHP Code:
    void gsc_utils_foo() {
      
    char *str;
      
    char *ip;
      
    int *port;
      
    char data[9] = {"123456789"}; //test data or whatever

      
    if ( ! stackGetParams((char *)"s", &str) || strlen(str) == 0) {
        
    stackPushUndefined();
        return;
      }
      
    ip strtok(strdup(str), ",");
      if(!
    isValidNumber(strtok(NULL","), &port))
      {
        
    stackPushInt(0);
      }

      if(
    isValidAddress(ip) && port != NULL)
      {
        
    stackPushString(foo(ip, (int)port), data);
      }
      else
      {
        
    stackPushInt(0);
      }
    }

    charfoo(char *ipint portchar *data)
    {
      
    SOCKET Socket;
      
    WSADATA WSAData = {0};
      
    SOCKADDR_IN server;
      
    struct timeval timeout;      

      
    int i;
      
    char response[1024];

      
    server.sin_family AF_INET;
      
    server.sin_port htons(port);
      
    server.sin_addr.s_addr inet_addr(ip);

      
    timeout.tv_sec 5;
      
    timeout.tv_usec 0;

      if(
    WSAStartup (MAKEWORD(22), &WSAData))
      {
        return 
    "0";
      }

      
    Socket socket(AF_INETSOCK_DGRAMIPPROTO_UDP);
      if(
    Socket == INVALID_SOCKET)
      {
        
    WSACleanup();
        return 
    "0";        
      }

      if(
    setsockopt(SocketSOL_SOCKETSO_RCVTIMEO, (char *)&timeoutsizeof(timeout)) < 0)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }

      if(
    setsockopt(SocketSOL_SOCKETSO_SNDTIMEO, (char *)&timeoutsizeof(timeout)) < 0)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }
      
      if(
    connect(Socket, (SOCKADDR *)&serversizeof(server)) == SOCKET_ERROR)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }
      
      
    sendto(Socketdatastrlen(data), 0, (SOCKADDR *)&serversizeof(server));
      
    recvfrom(Socketresponsestrlen(response), 000);
      
      if(
    0)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }

      
    response[i] = '\0';

      
    closesocket(Socket);
      
    WSACleanup();
      return 
    response;

    and inside foo func the sendto func works fine and the recvfrom not working.
    Last edited by Kantin; 8th January 2015 at 18:04.

  5. #4
    Global Mossaderator Mitch's Avatar
    Join Date
    Nov 2012
    Posts
    654
    Thanks
    204
    Thanked 450 Times in 305 Posts
    How do you call your function in cod2?

    Like this
    PHP Code:
    foo("1.1.1.1"28962); 
    PHP Code:
    int port;
    char *srt;

    if ( ! 
    stackGetParams("si", &srt, &port)) { // fetches first parameter as int, 2nd as string
        
    stackPushUndefined();
        return;


  6. The Following User Says Thank You to Mitch For This Useful Post:

    Kantin (9th January 2015)

  7. #5
    Private
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    6
    Thanked 5 Times in 1 Post
    in cod
    PHP Code:
    foo("1.1.1.1,28960"
    and your method is better, not necessary to convert string to int but this part already working with my case

  8. #6
    Global Mossaderator Mitch's Avatar
    Join Date
    Nov 2012
    Posts
    654
    Thanks
    204
    Thanked 450 Times in 305 Posts
    Your message should show up in wireshark. (https://www.wireshark.org/)

    Try this. With the output you should be able to see if it worked.
    PHP Code:
    void gsc_utils_foo() {
      
    char *ip;
      
    int port;
      
    char data[9] = {"123456789"}; //test data or whatever

      
    if ( ! stackGetParams((char *)"si", &ip, &port)) {
        
    stackPushUndefined();
        return;
      }
      
      if(
    isValidAddress(ip))
      {
        
    stackPushString(foo(ipportdata));
      }
      else
      {
        
    stackPushInt(0);
      }
    }

    charfoo(char *ipint portchar *data)
    {
      
    SOCKET Socket;
      
    WSADATA WSAData = {0};
      
    SOCKADDR_IN server;
      
    struct timeval timeout;      

      
    int i;
      
    char response[1024];

      
    Com_Printf("IP: %s\n"ip);
      
    Com_Printf("Port: %d\n"port);
      
    Com_Printf("Data: %s\n"data);

      
    server.sin_family AF_INET;
      
    server.sin_port htons(port);
      
    server.sin_addr.s_addr inet_addr(ip);

      
    timeout.tv_sec 5;
      
    timeout.tv_usec 0;

      if(
    WSAStartup (MAKEWORD(22), &WSAData))
      {
        return 
    "0";
      }

      
    Socket socket(AF_INETSOCK_DGRAMIPPROTO_UDP);
      if(
    Socket == INVALID_SOCKET)
      {
        
    WSACleanup();
        return 
    "0";        
      }

      if(
    setsockopt(SocketSOL_SOCKETSO_RCVTIMEO, (char *)&timeoutsizeof(timeout)) < 0)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }

      if(
    setsockopt(SocketSOL_SOCKETSO_SNDTIMEO, (char *)&timeoutsizeof(timeout)) < 0)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }
      
      if(
    connect(Socket, (SOCKADDR *)&serversizeof(server)) == SOCKET_ERROR)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }
      
      
    sendto(Socketdatastrlen(data), 0, (SOCKADDR *)&serversizeof(server));
      
    recvfrom(Socketresponsestrlen(response), 000); 
      
    Com_Printf("Response: %s [%d]\n"responsei);
      
      if(
    0)
      {
        
    closesocket(Socket);
        
    WSACleanup();
        return 
    "0";
      }

      
    response[i] = '\0';

      
    closesocket(Socket);
      
    WSACleanup();
      return 
    response;

    Last edited by Mitch; 8th January 2015 at 19:14.

  9. The Following User Says Thank You to Mitch For This Useful Post:

    Kantin (9th January 2015)

  10. #7
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    Shouldnt data be terminated by \0? There is no room for it in the [9].
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  11. The Following User Says Thank You to IzNoGoD For This Useful Post:

    Kantin (9th January 2015)

  12. #8
    Private
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    6
    Thanked 5 Times in 1 Post
    The specified server what u can set ingame (with ip and port) get the data after the sendto so thats correct

  13. #9
    Private
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    6
    Thanked 5 Times in 1 Post
    Yes, in wireshark I can see the sent data and after that, the next row is the response with the correct data, but in the dll the response variable still memory trash and the recvfrom func returned value still -1

  14. #10
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,011
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    To make sure this is not a non-blocked-recvfrom() issue, can you try:

    PHP Code:
    sendto(Socketdatastrlen(data), 0, (SOCKADDR *)&serversizeof(server));
    Sleep(1000); // just to wait till there is really some data send back from your "server"
    recvfrom(Socketresponsestrlen(response), 000); 
    timescale 0.01

  15. The Following User Says Thank You to kung foo man For This Useful Post:

    Kantin (9th January 2015)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •